package com.woniuxy.woniuticket.moviemodule.controller;

import com.woniuxy.woniuticket.movieinfomodule.service.CinemaInfoService;
import com.woniuxy.woniuticket.moviemodule.dto.MoiveDTO;
import com.woniuxy.woniuticket.moviemodule.service.CinemaMoviesService;
import com.woniuxy.woniuticket.moviemodule.service.MovieinfoService;
import com.woniuxy.woniuticket.ordermodule.service.CommentService;
import com.woniuxy.woniuticket.pojo.CinemaInfo;
import com.woniuxy.woniuticket.pojo.CinemaMovies;
import com.woniuxy.woniuticket.pojo.MoviesComment;
import com.woniuxy.woniuticket.pojo.UsersInfo;
import com.woniuxy.woniuticket.resultcommon.JsonResult;
import com.woniuxy.woniuticket.statisticsmodule.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 赵晓东 on 2019/6/18/018.
 */
@Controller
@RequestMapping("/movie")
public class CinemaMoviesController {
    @Resource
    private CinemaMoviesService cinemaMoviesService;

    @Autowired
    private MovieinfoService movieinfoService;

    @Resource
    private StatisticsService statisticsService;

    @Resource
    private CommentService commentService;

    @Resource
    private CinemaInfoService cinemaInfoService;

    @RequestMapping("/findAll")
    @ResponseBody
    public JsonResult findAll(HttpSession session){
        List<CinemaMovies> all = cinemaMoviesService.findAllByTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        List<MoiveDTO> list = new ArrayList<>();
        MoiveDTO dto = null;
        for (CinemaMovies movie:
             all) {
            dto = new MoiveDTO();
            dto.setCinemaMovies(movie);
            dto.setComment(statisticsService.avgStar(movie.getId()));
            list.add(dto);
        }
        return JsonResult.ok("查询成功",list);
    }

    @RequestMapping("/findMovieById")
    public ModelAndView findMovieById(Integer id,HttpSession session){
        CinemaMovies movie= cinemaMoviesService.findMovieById(id);
        CinemaInfo cinemaInfo = cinemaInfoService.selectCinemaInfo().get(0);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/movieshow/details");

        //判断当前用户是否登录，没有登录则不能回复
        Object userObj = session.getAttribute("user");
        System.out.println("用户对象："+userObj);
        System.out.println(userObj!=null);
        if(userObj!=null){
            modelAndView.addObject("loginstatus","已登录"); //设置登录状态
        }else{
            modelAndView.addObject("loginstatus","未登录"); //设置登录状态
        }
       if(movie!=null){
           modelAndView.addObject("movie",movie);
           modelAndView.addObject("star",statisticsService.avgStar(movie.getId()));
           List<MoviesComment> moviesComments = commentService.findCommentsByMovieId(id);
           //存储对应电影的评论信息
           modelAndView.addObject("moviesComments", moviesComments);
           modelAndView.addObject("cinemaInfo", cinemaInfo);
           String[] coord = cinemaInfo.getCoordinate().split(",");
           modelAndView.addObject("longitude", coord[0]);
           modelAndView.addObject("latitude", coord[1]);
       }
        return modelAndView;
    }

    @RequestMapping("/selectOne")
    @ResponseBody
    public Object selectOne(Integer moviesId){
        return cinemaMoviesService.findMovieById(moviesId);
    }


}
